//引入
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');//打印请求的日志

//引入模块化路由
var indexRouter = require('./routes/index');
var listRouter = require('./routes/list');
var courseRouter = require('./routes/course');
var loginRouter = require('./routes/login');
var personRouter = require('./routes/person');

//创建服务应用
var app = express();//一个函数

// view engine setup 设置模板引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

//使用中间件
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//开放静态资源
app.use('/uploads',express.static(path.join(__dirname, 'uploads')));

//为路由加前缀 使用路由
app.use('/api', indexRouter);
app.use('/api', listRouter);
app.use('/api', courseRouter);
app.use('/api', loginRouter);
app.use('/api', personRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  res.send({
    msg: 'Not Found',
    status: 404
  })
});

// error handler
app.use(function(err, req, res, next) {
    res.send(err)
});

module.exports = app;
